package com.hdculture.simple.expensemanager.premium;

import java.util.ArrayList;

import com.hdculture.simple.expensemanager.premium.R;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Bundle;
import android.content.Intent;
import android.database.Cursor;

public class ManageIncomeActivity_IncomeSources extends Activity {
    /** Called when the activity is first created. */
	Button backButton;
	ListView incomeSourceList;
	CoreDBAdapter dbAdapter;
	ArrayList<String> sources;
	String tag = "IncomeSources";
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
    	dbAdapter.close();
    	Log.i(tag,"DB Connection Closed");
		super.onPause();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		dbAdapter.open();
		Log.i(tag,"DB Connection Resumed");
		super.onResume();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.incomesources);
        incomeSourceList = (ListView)findViewById(R.id.incomeSourceList);
        backButton = (Button)findViewById(R.id.backButton);
        dbAdapter= new CoreDBAdapter(this);
        dbAdapter.open();
        Log.i(tag,"DB Connection Open");
        try{
        	Cursor c = dbAdapter.fetchAllIncomeSources();
        	startManagingCursor(c);
        	c.moveToFirst();
        	sources = new ArrayList<String>();
        	while(c.isAfterLast()==false){
        		sources.add(c.getString(0));
        		c.moveToNext();
        	}
        	String[] sourceArray = new String[sources.size()];
        	for(int i=0;i<sources.size();i++){
        		sourceArray[i]=sources.get(i);
        	}
        	System.out.println(sources);
        	incomeSourceList.setAdapter(new ArrayAdapter<String>(ManageIncomeActivity_IncomeSources.this,android.R.layout.simple_list_item_1,sourceArray));
        	incomeSourceList.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					String source=sources.get(arg2);
					Intent i = new Intent(ManageIncomeActivity_IncomeSources.this,IncomeSourceDetailActivity.class);
					i.putExtra("SOURCE", source);
					startActivity(i);
				}
			});
        	
        }catch(Exception ex){
        	ex.printStackTrace();
        	
        }
        backButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
    }
}